package com.victor.zjbj_android.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.util.Log;

import com.blankj.utilcode.util.ActivityUtils;
import com.victor.zjbj_android.bean.LoginConfig;
import com.victor.zjbj_android.mod.login.LoginActivity;



import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by liuwei on 2017/10/23.
 * 共有的工具类用来获取各种封装信息
 */
public class MainUtils {
    public static void showLoginDialog(Context mContext){
        LoginConfig.setLoginConfig(
                LoginConfig.getInstance()
                        .setAutoLogin(false)
                        .setAppToken(null)
                        .setuid(null)
                        .setUsername(null)
                        );
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setMessage("登录失效,点击确定按钮登录!");
        builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ActivityUtils.finishAllActivities();
                mContext.startActivity(new Intent(mContext, LoginActivity.class));
                dialog.dismiss();
            }
        });
        builder.setCancelable(false);
        builder.show();
    }

    /**
     * 获取状态栏高度
     *
     * @return
     */
    public static int getStatusBarHeight(Activity activity) {
        try {
            int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                return activity.getResources().getDimensionPixelSize(resourceId);
            }
        } catch (Exception e) {
        }
        return 0;
    }

    //不能连续点击
    private static long lastClickTime;

    public synchronized static boolean isFastClick() {
        long time = System.currentTimeMillis();
        if (time - lastClickTime < 1500) {
            return true;
        }
        lastClickTime = time;
        return false;
    }

    /**
     * @Auth Luke
     * @Date 11.30
     * 返回是否登录
     **/
    public static boolean isOnline(Context mContext) {
        if (LoginUtils.getLoginConfig(mContext) == null) {
            return false;
        } else if (LoginUtils.getLoginConfig(mContext).isAutoLogin()) {
            return true;
        } else if (!LoginUtils.getLoginConfig(mContext).isAutoLogin()) {
            return false;
        }
        return false;
    }
    public static String getToken(Context mContext){
        if (LoginUtils.getLoginConfig(mContext) == null) {
            return "";
        } else {
            return LoginUtils.getLoginConfig(mContext).getAppToken();
        }
    }
    /**
     * @Auth Luke
     * @Date 11.30
     * 跳转登录或者对应的页面
     **/
//    public static void toLogin(Activity context, Class<?> cls){
//        if (!isOnline(context)){
//
//            context.overridePendingTransition(R.anim.from_top_in,
//                    R.anim.from_top_out);
//        }else {
//            context.startActivity(new Intent(context,cls).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
//            context.overridePendingTransition(R.anim.from_right_to_left_in,
//                    R.anim.from_left_to_right_out);
//        }
//    }
//    /**
//     * @Date 11.18
//     * 纯跳转登录
//     **/

//    public static void intentToLogin(Activity context) {
//
//        context.overridePendingTransition(R.anim.from_right_to_left_in,
//                R.anim.from_left_to_right_out);
//        context.finish();
//    }
    public static boolean isMobileNO(String mobiles) {

        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(17[0-7])|(18[0-9]))\\d{8}$");

        Matcher m = p.matcher(mobiles);

        System.out.println(m.matches() + "---");

        return m.matches();

    }


    private static void popFragment(Activity activity, FragmentManager fm) {
        int count = fm.getBackStackEntryCount();
        Log.e("LeftPressed", "count:>>>>>>>>>>>>>>>>>>>>" + count);
        for (int i = 0; i < count; i++) {
            Log.e("LeftPressed", fm.getBackStackEntryAt(i).getName() + "");
        }
        if (count > 1) {
            fm.popBackStackImmediate(null, 0);
        } else {
            activity.finish();
        }
    }
    /****
     * 判断字符串是否json格式
     ****/
    public static boolean isJsonString(String jsonStr) {
        if (jsonStr != null && jsonStr.startsWith("{")) {
            return true;
        }
        return false;
    }

}
